import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';

import { applyMiddleware, legacy_createStore as createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import rootReducer from './reducer';

const store = createStore(rootReducer, composeWithDevTools(applyMiddleware(thunk)));
export default store;

export type RootState = ReturnType<typeof store.getState>;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

export type AppDispatch = typeof store.dispatch;
// export const useAppDispatch = () => useDispatch<AppDispatch>();

// 💥  临时解决办法： AppDispatch 换成 any， 解决redux-thunk和redux冲突
// 🔔 https://github.com/reduxjs/redux-thunk/issues/333
export const useAppDispatch = () => useDispatch<any>();
